<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="common/include :: header('客户联系人列表')"/>
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="post-form">
                <div class="select-list">
                    <ul>
                        <li>
                            关键字：<input type="text" name="keyword"/>
                        </li>
                        <li>
                            状态：
                            <select name="auditState" th:with="type=${@dict.getType('bus_audit_state')}">
                                <option value="">请选择</option>
                                <option th:each="dict : ${type}" th:text="${dict.label}"
                                        th:value="${dict.value}"></option>
                            </select>
                        </li>
                        <li class="select-time">
                            <label>录入时间： </label>
                            <input type="text" class="time-input" id="startTime" placeholder="开始时间" name="beginTime"/>
                            <span>-</span>
                            <input type="text" class="time-input" id="endTime" placeholder="结束时间" name="endTime"/>
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i
                                    class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i
                                    class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>

        <div class="btn-group-sm" id="toolbar" role="group">
            <a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="system:post:add">
                <i class="fa fa-plus"></i> 新增
            </a>


        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table"></table>
        </div>

    </div>
</div>
<th:block th:include="common/include :: footer"/>
<script th:inline="javascript">
    var auditDatas = [[${@dict.getType('bus_audit_state')}]];  //审核状态
    var prefix = ctx + "/business/contract";
    $(function () {
        var options = {
            url: prefix + "/query",
            createUrl: prefix + "/addPage",
            updateUrl: prefix + "/editPage?id={id}",
            removeUrl: prefix + "/remove",
            modalName: "联系人列表",
            columns: [{
                checkbox: true
            },
                {
                    field:'no',
                    title:'序号',
                    formatter:function (value,row,index) {
                        //获取每页显示的数量
                        var pageSize = $('#bootstrap-table').bootstrapTable('getOptions').pageSize;
                        //获取当前是第几页
                        var pageNumber = $('#bootstrap-table').bootstrapTable('getOptions').pageNumber;
                        //返回序号
                        return pageSize * (pageNumber - 1) + index + 1;
                    }
                },
                {
                    field: 'id',
                    title: '合同id',
                    visible: false
                },
                {
                    field: 'customer.customerName',
                    title: '客户',
                    sortable: true
                },
                {
                    field: 'contractName',
                    title: '合同名称',
                    sortable: true
                },

                {
                    field: 'contractCode',
                    title: '合同编号',
                    sortable: true
                },
                {
                    field: 'amounts',
                    title: '合同金额(单位:万元)',
                    sortable: true
                },
                {
                    field: 'startDate',
                    title: '合同开始日期',
                    sortable: true
                },
                {
                    field: 'endDate',
                    title: '合同结束时间',
                    sortable: true
                },
                {
                    field: 'appendix',
                    title: '电子附件',
                    sortable: true,
                    formatter:function (value,row,index) {
                        return '<a class="btn btn-warning" onclick="downloadFile('+row.id+')" shiro:hasPermission="system:post:add">' +
                            '<i class="fa fa-download" aria-hidden="true"></i>下载</a>';
                    }
                },
                {
                    field: 'user.userName',
                    title: '录入人',
                    sortable: true
                },
                {
                    field: 'inputTime',
                    title: '录入时间',
                    sortable: true
                },
                {
                    field: 'auditState',
                    title: '审核状态',
                    sortable: true,
                    formatter: function(value, item, index) {
                        return $.table.selectDictLabel(auditDatas, value);
                    }
                },
                {
                    field: 'affixSealState',
                    title: '是否盖章',
                    sortable: true,
                    formatter:function (value,item,index) {
                        return value ? '是' : '否'
                    }
                },
                {
                    field: 'nullifyState',
                    title: '是否作废',
                    formatter:function (value,item,index) {
                        return value ? '否' : '是'
                    }
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var actions = [];
                        //按钮显示样式
                        let editClass = row.auditState != 1 && row.nullifyState ? '' : 'hidden';   //审核不为通过,且不作废
                        let auditClass = row.auditState == 0 && row.nullifyState ? '' : 'hidden';  //审核为审核中,且不为作废
                        let affixSealClass = row.auditState == 1 && row.nullifyState && row.affixSealState == 0? '' : 'hidden';  //审核为通过,且不为作废
                        let nullifyClass = !row.affixSealState && row.nullifyState ? '' : 'hidden';  //不盖章,且不为作废
                        actions.push('<a class="btn btn-success '+editClass+'  btn-xs " href="javascript:void(0)" ' +
                            'onclick="$.operate.edit(\'' + row.id + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                        actions.push('<a class="btn btn-info '+auditClass+'  btn-xs " href="javascript:void(0)" ' +
                            'onclick="auditON('+row.id+')"><i class="fa fa-check-square-o"></i>审核通过</a> ');
                        actions.push('<a class="btn btn-warning '+auditClass+' btn-xs " href="javascript:void(0)" ' +
                            'onclick="auditOff('+row.id+')"><i class="fa fa-check-square-o"></i>审核不通过</a> ');
                        actions.push('<a class="btn btn-primary '+affixSealClass+'  btn-xs " href="javascript:void(0)" ' +
                            'onclick="affixSealOn('+row.id+')"><i class="fa fa-check-square-o"></i>确认盖章</a> ');
                        actions.push('<a class="btn btn-danger '+nullifyClass+' btn-xs " href="javascript:void(0)" ' +
                            'onclick="isNullify(\'' + row.id + '\')"><i class="fa fa-check-square-o"></i>作废</a> ');
                        return actions.join('');
                    }
                }]
        };
        $.table.init(options);
    });

    //编辑
    $("#bootstrap-table").on("check.bs.table uncheck.bs.table check-all.bs.table uncheck-all.bs.table load-success.bs.table", function () {
        var rows = $('#bootstrap-table').bootstrapTable('getSelections'); //获取bootstrap-table中选中的数据
        //如果选中多条数据则不可点击
        if (rows.length != 1) {
            $("#editCL").addClass("disabled");
            return;
        }
        $("#editCL").removeClass("disabled");
    });

    function editCL() {
        var rows = $('#bootstrap-table').bootstrapTable('getSelections');
        var row = rows[0];
        $.modal.open("编辑联系人信息","/business/customerLinkMane/editPage?id="+row.id);
    }

    //文件下载
    function downloadFile(id) {
        $.modal.confirm("是否下载附件",function () {
            $.operate.get(prefix+"/downloadFile?id="+id,function (result) {
                console.log(result.data);
                window.open(result.data,"下载",'top=10,left=10,width=400,height=200');
            });
        })
    }
    //审核通过
    function auditON(id) {
        $.modal.confirm("是否通过审核",function () {
            $.operate.get(prefix+"/auditON?id="+id);
        });

    }
    //审核不通过
    function auditOff(id) {
        $.modal.confirm("是否通过审核",function () {
            $.operate.get(prefix+"/auditOff?id="+id);
        });
    }
    //确认盖章
    function affixSealOn(id) {
        $.modal.confirm("是否盖章",function () {
            $.operate.get(prefix+"/affixSealOn?id="+id);
        });
    }
    //确认作废
    function isNullify(id) {
        $.modal.confirm("是否作废合同",function () {
            $.operate.get(prefix+"/isNullify?id="+id);
        });
    }
</script>
</body>
</html>